package com.hzncc.flowable_diboot.service.bpm.impl;

import cn.hutool.core.util.ObjectUtil;
import com.diboot.core.service.impl.BaseServiceImpl;
import com.hzncc.flowable_diboot.exception.BusinessException;
import com.hzncc.flowable_diboot.mapper.BpmProcessInstanceCopyMapper;
import com.hzncc.flowable_diboot.entity.bpm.BpmProcessInstanceCopy;
import com.hzncc.flowable_diboot.service.bpm.BpmProcessInstanceCopyService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
* 任务抄送 相关Service实现类
* @author MyName
* @version 1.0
* @date 2024-06-26
* Copyright © MyCorp
*/
@Slf4j
@Service
public class BpmProcessInstanceCopyServiceImpl extends BaseServiceImpl<BpmProcessInstanceCopyMapper, BpmProcessInstanceCopy> implements BpmProcessInstanceCopyService {
    
    @Resource
    private TaskService taskService;
    
    @Resource
    private RuntimeService runtimeService;
    
    @Resource
    private RepositoryService repositoryService;
    
    @Override
    @Transactional
    public void createBpmProcessInstanceCopy(Collection<String> copyUserIds, String taskId) {
        //检验任务
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (ObjectUtil.isNull(task)) {
            throw new BusinessException("任务不存在");
        }
        //检验流程实例
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
        if (ObjectUtil.isNull(processInstance)) {
            throw new BusinessException("流程实例不存在");
        }
        //检测流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstance.getProcessDefinitionId()).singleResult();
        if (ObjectUtil.isNull(processDefinition)){
            throw new BusinessException("流程定义不存在");
        }
        //创建抄送
        List<BpmProcessInstanceCopy> bpmProcessInstanceCopyList = copyUserIds.stream().map(copyUserId -> {
            BpmProcessInstanceCopy bpmProcessInstanceCopy = new BpmProcessInstanceCopy();
            bpmProcessInstanceCopy.setInstanceId(processInstance.getProcessInstanceId())
                    .setInstanceName(processInstance.getName())
                    .setCategory(processDefinition.getCategory())
                    .setStartUserId(processInstance.getStartUserId())
                    .setTaskId(task.getId())
                    .setTaskName(task.getName())
                    .setCopyUserId(copyUserId);
            return bpmProcessInstanceCopy;
        }).collect(Collectors.toList());
        this.saveBatch(bpmProcessInstanceCopyList);
    }
}